(function(a){
    a.fn.slideshow=function(p){
        var p=p||{};

        var autoplay = p&&p.autoplay?p.autoplay:"enable";
        var time_interval = p&&p.time_interval?p.time_interval:"3000";
        var directory = p&&p.directory?p.directory:"images";
        var start = p&&p.start?p.start:0;
        var slide_data = p&&p.slide_data?p.slide_data:"";

        var current_box = 1;
        var timeout;
        var current_id = start;
        var image_count = slide_data.length;
        var g=a(this);
        if(image_count <= 1){
            g.children(".left").hide();
            g.children(".right").hide();
        }else{
            g.children(".left").fadeOut();
            g.children(".right").fadeOut();
            g.hover(
                function(){
                    g.children(".left").fadeIn();
                    g.children(".right").fadeIn();
                },
                function(){
                    g.children(".left").fadeOut();
                    g.children(".right").fadeOut();
                }
            );
        }
        g.children(".left").click(
            function(){
                previous();
            }
        );
        g.children(".right").click(
            function(){
                next();
            }
        );
        $("#project_more_btn").hover(
            function(){
                g.children(".hover_info").show();
            },
            function(){
                g.children(".hover_info").hide();
            }
        );
        play();
        function play(){
            clearTimeout(timeout);
            var tmp_img = new Image();
            tmp_img.onload = function(){
                
                $("#project_name").text(slide_data[current_id].title);
                g.children(".hover_info").html('<h2>'+slide_data[current_id].title+'</h2><p>'+slide_data[current_id].description+'</p>');
                g.children(".pic_box"+current_box).hide();
                g.children(".pic_box"+current_box).find(".pic_box_show").html('<img src="'+getImagePath(current_id)+'" />');
                g.children(".pic_box"+current_box).fadeIn();
                if(current_box == 1){
                    current_box = 2;
                }else{
                    current_box = 1;
                }
                g.children(".pic_box"+current_box).fadeOut();
                if(autoplay == "enable"){
                    if(image_count > 1){
                        timeout = setTimeout(next,time_interval);
                    }
                }
            };
            tmp_img.src = getImagePath(current_id);
        }
        function next(){
            current_id ++;
            if(current_id >= image_count){
                current_id = 0;
            }
            play();
        }
        function previous(){
            current_id --;
            if(current_id < 0){
                current_id = image_count - 1;
            }
            play();
        }
        function getImagePath(index){
            return directory+slide_data[index].image;
        }
    };
})(jQuery);